#include "StompedReality/Core/FileSystem.h"
#include <io.h>
#include <windows.h>
namespace StompedReality
{
namespace Utility
{
std::shared_ptr<FileInfoList> FileSystem::findFiles ( FileFilter filter, std::string sub )
{
  std::shared_ptr<FileInfoList> fileList( new FileInfoList( ));
  _finddata_t find_data;
  intptr_t find_handle = _findfirst ( ( this->m_BasePath + "/" + sub + "/*" ).c_str( ), &find_data );

  if ( find_handle == -1 )
  {
    return fileList;
  }
  do
  {
    if ( find_data.attrib == _A_SUBDIR )
    {
      if ( strcmp ( find_data.name, "." ) == 0 || strcmp ( find_data.name, ".." ) == 0 )
      {
        continue;
      }
      std::shared_ptr<FileInfoList> subRes = this->findFiles ( filter,
                                                               sub + ( ( sub.empty( ) ) ? "" : "/" ) + find_data.name );
      fileList->insert ( fileList->end( ), subRes->begin( ), subRes->end( ) );
    }
    else
    {
      if ( filter ( find_data.name ) )
        fileList->push_back ( FileInfo ( find_data.name, sub ) );
    }
  }
  while ( _findnext ( find_handle, &find_data ) == 0 );
  _findclose ( find_handle );
  return fileList;
}

bool FileSystem::createDirectory ( std::string dir )
{
  return CreateDirectory( dir.c_str( ), NULL );
}

}
}
